<?php

namespace App\Sidong\Controllers;

use App\Models\MemberCard;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use Encore\Admin\Show;

class MemberCardController extends AdminController
{
    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = '会员卡管理';


    public function index(Content $content)
    {
        return $content
            ->title($this->title())
            ->breadcrumb(['text' => '会员卡列表'])
            ->body($this->grid());
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new MemberCard);
        $admin = \Admin::user();
        $grid->model()->where('tenant_id', $admin->tenant_id);
        $grid->disableBatchActions();
        $grid->disableExport();
        $grid->actions(function ($actions) {
            // 去掉查看
            $actions->disableView();
        });

        $grid->quickSearch('name', 'remark');
        $grid->column('id', 'ID');
        $grid->column('name', '名称');
        $grid->column('cover_url', '封面图')->lightbox(['width' => 35, 'height' => 35]);
        $grid->column('type', '类型')->using([1 => '计时卡', 2 => '计次卡']);
        $grid->column('days', '有效期(天)');
        $grid->column('count', '有效次数(次)');
        $grid->column('price', '价格');
        $grid->column('market_price', '市场价');
        $states = [
            'on'  => ['value' => 1, 'text' => '启用', 'color' => 'success'],
            'off' => ['value' => 2, 'text' => '禁用', 'color' => 'danger'],
        ];
        $grid->column('is_enable', '启用状态')->switch($states);
        $grid->column('remark', '备注');
        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(MemberCard::findOrFail($id));



        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $admin = \Admin::user();
        $form = new Form(new MemberCard);
        $form->hidden('tenant_id')->default($admin->tenant_id);
        $form->text('name', '会员卡名称')
            ->addElementClass('member-card-name')
            ->placeholder('请输入会员卡名称，如：月卡（买一送五）')
            ->help('小程序会员卡列表展示的名称，字符请控制在8个汉字以内')
            ->rules('required');
        $form->radio('type', '会员卡类型')
            ->options(['1' => '计时卡', '2'=> '计次卡'])
            ->default('1')
            ->addElementClass('member-card-type')
            ->rules('required');
        $form->number('days', '有效天数')
            ->placeholder('请输入有效天数')
            ->min(1)
            ->default(1)
            ->addElementClass('member-card-days')
            ->help('当会员卡类型为计时卡时输入有效天数，用户购买后按照天数计算会员有效期');
        $form->number('count', '有效次数')
            ->placeholder('请输入有效次数')
            ->min(1)
            ->default(1)
            ->addElementClass(['member-card-count'])
            ->help('当会员卡类型为计次卡时输入有效次数，用户购买后按照次数计算，有效期不限（除非购买了计时会员卡)');
        $form->currency('price', '价格')
            ->placeholder('请输入会员卡价格')
            ->help('小程序会员卡列表展示的价格，为最终用户实际支付的价格，如需输入小数请在数字后继续输入点号即可。')
            ->width('200px')
            ->symbol('￥')
            ->rules('required');
        $form->currency('market_price', '市场价')
            ->placeholder('请输入会员卡市场价')
            ->help('小程序会员卡列表展示的市场价，用横线划掉，如需输入小数请在数字后继续输入点号即可。')
            ->width('200px')
            ->symbol('￥')
            ->rules('required');
        $form->image('cover_url', '会员卡封面图')
            ->placeholder('请上传会员卡封面图片')
            ->help('小程序会员卡列表展示的封面图片，只支持 png、jpg、jpeg 类型的图片，大小不超过1M，尺寸建议为：128*128')
            ->uniqueName()
            ->rules('required');
        $form->textarea('remark', '备注')
            ->placeholder('请输入会员卡备注信息')
            ->help('小程序会员卡列表展示的会员卡的简要介绍，为了页面美观请填写不超过28个汉字')
            ->rules('required');
        $states = [
            'on'  => ['value' => 1, 'text' => '启用', 'color' => 'success'],
            'off' => ['value' => 0, 'text' => '禁用', 'color' => 'danger'],
        ];
        $form->switch('is_enable', '是否启用')
            ->default(1)
            ->help('注意：如果选择禁用则会员卡不会出现在小程序上的会员卡列表中。')
            ->states($states);
        $script = <<<SCRIPT
$('.member-card-count').parents('.form-group').hide();
$('.member-card-type').on('ifChecked', function(event){
  var value = $(this).val();
  if (value === '1') {
    $('.member-card-days').val(1);
    $('.member-card-count').val(0);
    $('.member-card-days').parents('.form-group').show();
    $('.member-card-count').parents('.form-group').hide();
  } else if (value === '2') {
    $('.member-card-days').val(0);
    $('.member-card-count').val(1);
    $('.member-card-days').parents('.form-group').hide();
    $('.member-card-count').parents('.form-group').show();
  }
});

SCRIPT;

        \Admin::script($script);
        return $form;
    }
}
